import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { HttpClient } from '@angular/common/http';
import { EntityDtoOfInt64, UserListDto, TaskServiceProxy, CodeRuleServiceProxy, CodeRuleEditDto } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { CreateoreditcoderulemodalComponent } from './createoreditcoderulemodal/createoreditcoderulemodal.component';
import { finalize } from 'rxjs/operators';
import { from } from 'rxjs';

@Component({
  selector: 'app-coderule',
  templateUrl: './coderule.component.html',
  styleUrls: ['./coderule.component.css']
})
export class CoderuleComponent extends AppComponentBase {

  @ViewChild('createoreditcoderulemodal') createoreditcoderulemodal: CreateoreditcoderulemodalComponent;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  filterText = '1';
  selectedPermission = '';
  role = '';
  code = 'encode';
  onlyLockedUsers = false;
  advancedFiltersAreShown = false;
  constructor(
    injector: Injector,
    private _taskServiceProxy: TaskServiceProxy,
    private _fileDownloadService: FileDownloadService,
    private _activatedRoute: ActivatedRoute,
    private _coderolesService: CodeRuleServiceProxy,
    private _httpClient: HttpClient
  ) {
    super(injector);
    this.filterText = this._activatedRoute.snapshot.queryParams['filterText'] || '';
  }

  createCodeRule(): void {
    this.createoreditcoderulemodal.show();
  }

  deletecoderule(dto?: CodeRuleEditDto): void {
    console.log(dto)
    this.message.confirm(
      this.l('UserDeleteWarningMessage', dto.fullName),
      this.l('AreYouSure'),
      isConfirmed => {
        if (isConfirmed) {
          this._coderolesService
            .removeCodeRule(dto.id)
            .subscribe(() => {
              this.reloadPage();
              this.notify.success(this.l('SuccessfullyDeleted'));
            })
        }
      }
    );
  }
  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }
  getCodeRules(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();

    this._coderolesService.getCodeRules(
      this.filterText,
      this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event),
    ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.primengTableHelper.records = result.items;
      console.log(result);
      this.primengTableHelper.hideLoadingIndicator();
    });
  }

}
